//
//  TribalSelectedBox.m
//  PigMarket
//
//  Created by 张敬 on 2019/9/4.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "TribalSelectedBox.h"
#import "TribalSelectedBoxCell.h"
#import "SelectTribes.h"


//展示Viwe高度
#define kCategoryHeight 421

@interface TribalSelectedBox ()<UITableViewDelegate, UITableViewDataSource>

/**
 MeatShopCartBox 's TableView -> UITableView
 */
@property (nonatomic, strong) UITableView *tableView;


/**
 MeatShopCartBox 's Header -> UIView
 */
@property (nonatomic, strong) UIView *header;

@property (nonatomic, strong) UILabel *detailLab;

@property (nonatomic, assign) float totalHeight;

@property (nonatomic, strong) TribalSelectedBoxCell *aimsCell;
@property (nonatomic, strong) id aimsItem;
@property (nonatomic, assign) NSInteger aimIndex;


@end



@implementation TribalSelectedBox
- (void)setupUI{
    
    [super setupUI];
    
    _aimsCell = nil;
    _aimsItem = nil;
    _aimIndex = -1;
    
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    self.contentView.backgroundColor = KClearColor;
    [self.contentView addSubview:self.tableView];
    
}

- (void)setupContentLayout{
    
    self.height = KScreenHeight;
    
    CGFloat height = self.totalHeight;
    
    if (height > kCategoryHeight) {
        height = kCategoryHeight;
    }
    
    {
        self.contentView.mj_x = 0;
        self.contentView.mj_y = KScreenHeight  - height;
        self.contentView.width = KScreenWidth;
        self.contentView.height = height;
    }
    
    {
        self.tableView.frame = CGRectMake(0, 0, KScreenWidth, height);
    }

}


#pragma mark - tableview delegate/dataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return section == 0 ?1 :self.datas.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TribalSelectedBoxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TribalSelectedBoxCell"];
    cell.type = @(indexPath.section);
    
    if (indexPath.row < self.datas.count && indexPath.section == 1) {
        id  item = self.datas[indexPath.row];
        cell.item = item;
     }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row < self.datas.count && indexPath.section == 1) {
        
        id obj = self.datas[indexPath.row];
        TribalSelectedBoxCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if ([obj isKindOfClass: [SelectTribes class] ]) {
            SelectTribes *item = (SelectTribes *)obj;
            BOOL isOpen = [item.isOpen integerValue] == 1;
            [self updateCellByIsOpen:isOpen cell:cell item:item index:indexPath.row];
        }
        
        if ([obj isKindOfClass: [CityItem class] ]) {

            CityItem *item  = (CityItem *)obj;
            BOOL isOpen = [item.isOpen integerValue] == 1;
            [self updateCellByIsOpen:isOpen cell:cell item:item index:indexPath.row];
        }

        if ([obj isKindOfClass: [AreaItem class] ]) {

            AreaItem *item = (AreaItem *)obj;
            BOOL isOpen = [item.isOpen integerValue] == 1;
            [self updateCellByIsOpen:isOpen cell:cell item:item index:indexPath.row];
            
        }

        if ([obj isKindOfClass: [TribeItem class] ]) {

           TribeItem* item = (TribeItem *)obj;
            BOOL isOpen = [item.isOpen integerValue] == 1;
            [self updateCellByIsOpen:isOpen cell:cell item:item index:indexPath.row];
        }
        
    }
}

- (void)updateCellByIsOpen:(BOOL)isOpen cell:(TribalSelectedBoxCell *)cell item:(id)item index:(NSInteger)index
{
    if (isOpen) {
        
        if (self.aimsCell != nil) {
            self.aimsCell.icon.image = [UIImage imageNamed:@"adopt_adoptdetail_agreement"];
            self.aimsCell.titleLab.textColor = UIColorFromRGB(0x999999);
            self.aimsCell.titleLab.font = mFont(15);
        }
        
        cell.icon.image = [UIImage imageNamed:@"adopt_adoptdetail_agreement_selected"];
        cell.titleLab.textColor = mMainColor;
        cell.titleLab.font = mMediumFont(16);
        self.aimsCell = cell;
        
        self.aimsItem = item;
        self.aimIndex = index;
    }
}



#pragma mark - setter

- (void)setDatas:(NSArray *)datas{
    
    _datas = datas;
    self.totalHeight = 54*datas.count +52*2 + TJ_StatusBarHeight_Define;
    [self setupContentLayout];
}


#pragma mark - getter

- (UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.separatorColor = KRedColor;
        _tableView.rowHeight = 54;
        _tableView.tableHeaderView = self.header;
        [_tableView registerClass:[TribalSelectedBoxCell class] forCellReuseIdentifier:@"TribalSelectedBoxCell"];
        
        UIView *footer = [UIView new];
        footer.frame = CGRectMake(0, 0, KScreenWidth, TJ_StatusBarHeight_Define);
        footer.backgroundColor = KWhiteColor;
        _tableView.tableFooterView = footer;
        
    }
    return _tableView;
}


- (UIView *)header{
    if (!_header) {
        _header = [[UIView alloc] init];
        _header.backgroundColor = KWhiteColor;
        _header.frame = CGRectMake(0, 0, KScreenWidth, 52);
        [self drawCornerByView:_header];
        
        UILabel *titleLab = [UILabel makeLabelWithText:@"选择省份" textColor:UIColorFromRGB(0x24253D) font:mFont(16) textAliment:0];
        titleLab.frame = CGRectMake(15, 0, KScreenWidth/2.0, 52);
        [_header addSubview:titleLab];
        
        _detailLab = [UILabel makeLabelWithText:@"确定" textColor:mMainColor font:mFont(16) textAliment:2];
        _detailLab.frame = CGRectMake(0, 0, KScreenWidth/2.0, 52);
        _detailLab.mj_x = KScreenWidth - _detailLab.width - 15;
        
        @weakify(self);
        _detailLab.tapBlock = ^{
            @strongify(self);
            
            if (self.aimsItem == nil)
            {
                return;
            }
            
            if (self.selectBlock) {
                self.selectBlock(self.aimsItem, self.aimIndex);
            }
            
           [kNotificationCenter postNotificationName:KMeatMarketCartBoxHiddenNotification object:nil];
        };
        
        [_header addSubview:_detailLab];
        
    }
    return _header;
}


- (void)drawCornerByView:(UIView *)view{
    
    CGRect rect = CGRectMake(0, 0, view.width, 52);
    UIRectCorner corners = UIRectCornerTopLeft|UIRectCornerTopRight;
    UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:CGSizeMake(8, 8)];
    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    _header.layer.mask = shape;
}

#pragma mark - other methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 34;
    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

@end


